home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / QuickTime / QuickTime 3 Interfaces & Libs / QTDevWin / CIncludes / Math64.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-21  |  11.5 KB  |  378 lines  |  [TEXT/dosa]

  1. /*
  2.      File:        Math64.h
  3.  
  4.      Contains:    64-bit integer math Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Release:    QuickTime 3.0
  8.  
  9.      Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Bugs?:        Please include the the file and version information (from above) with
  12.                  the problem description.  Developers belonging to one of the Apple
  13.                  developer programs can submit bug reports to:
  14.  
  15.                      devsupport@apple.com
  16.  
  17. */
  18. #ifndef __MATH64__
  19. #define __MATH64__
  20.  
  21. #ifndef __CONDITIONALMACROS__
  22. #include <ConditionalMacros.h>
  23. #endif
  24. #ifndef __MACTYPES__
  25. #include <MacTypes.h>
  26. #endif
  27.  
  28.  
  29.  
  30. #if PRAGMA_ONCE
  31. #pragma once
  32. #endif
  33.  
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37.  
  38. #if PRAGMA_IMPORT
  39. #pragma import on
  40. #endif
  41.  
  42. #if PRAGMA_STRUCT_ALIGN
  43.     #pragma options align=mac68k
  44. #elif PRAGMA_STRUCT_PACKPUSH
  45.     #pragma pack(push, 2)
  46. #elif PRAGMA_STRUCT_PACK
  47.     #pragma pack(2)
  48. #endif
  49.  
  50.  
  51. /*--------------------------------------------------------------------------------
  52.                 These routines are intended to provide C software support for
  53.                 64 bit integer types.  Their behavior should mimic anticipated
  54.                 64 bit hardware. This implementation should replace use of the
  55.                 "wide" type found in PowerPC.
  56.  
  57.     The following routines are available for performing math on 64-bit integers:
  58.     
  59.     S64Max
  60.                 Returns the largest representable SInt64.
  61.     S64Min
  62.                 Returns the smallest (i.e. most negative) SInt64.  Note: the negative
  63.                 (absolute value) of this number is not representable in an SInt64.
  64.                 That means that S64Negate(S64Min) is not representable (in fact,
  65.                 it returns S64Min).
  66.     S64Add
  67.                 Adds two integers, producing an integer result.  If an overflow
  68.                 occurs the result is congruent mod (2^64) as if the operands and
  69.                 result were unsigned.  No overflow is signaled.
  70.     
  71.     S64Subtract
  72.                 Subtracts two integers, producing an integer result.  If an overflow
  73.                 occurs the result is congruent mod (2^64) as if the operands and
  74.                 result were unsigned.  No overflow is signaled.
  75.  
  76.     S64Negate
  77.                 Returns the additive inverse of a signed number (i.e. it returns
  78.                 0 - the number).  S64Negate (S64Min) is not representable (in fact,
  79.                 it returns S64Min).
  80.     
  81.     S64Absolute
  82.                 Returns the absolute value of the number (i.e. the number if
  83.                 it is positive, or 0 - the number if it is negative).
  84.                 See S64Negate above.
  85.                 
  86.     S64Multiply
  87.                 Multiplies two signed numbers, producing a signed result.  Overflow
  88.                 is ignored and the low-order part of the product is returned.  The
  89.                 sign of the result is not guaranteed to be correct if the magnitude
  90.                 of the product is not representable.
  91.                 
  92.     S64Div
  93.                 Divides dividend by divisor, returning the quotient.  
  94.  
  95.     S64Divide
  96.                 Divides dividend by divisor, returning the quotient.  The remainder
  97.                 is returned in *remainder if remainder (the pointer) is non-NULL.
  98.                 The sign of the remainder is the same as the sign of the dividend
  99.                 (i.e. it takes the absolute values of the operands, does the division,
  100.                 then fixes the sign of the quotient and remainder).  If the divisor
  101.                 is zero, then S64Max() will be returned (or S64Min() if the dividend
  102.                 is negative), and the remainder will be the dividend; no error is
  103.                 reported.
  104.     
  105.     S64Set
  106.                 Given an SInt32, returns an SInt64 with the same value.  Use this
  107.                 routine instead of coding 64-bit constants (at least when the
  108.                 constant will fit in an SInt32).
  109.     
  110.     S64SetU
  111.                 Given a UInt32, returns a SInt64 with the same value.
  112.     
  113.     S64Compare
  114.                 Given two signed numbers, left and right, returns an
  115.                 SInt32 that compares with zero the same way left compares with
  116.                 right.  If you wanted to perform a comparison on 64-bit integers
  117.                 of the form:
  118.                         operand_1 <operation> operand_2
  119.                 then you could use an expression of the form:
  120.                         xxxS64Compare(operand_1,operand_2) <operation> 0
  121.                 to test for the same condition.
  122.                 
  123.                 CAUTION: DO NOT depend on the exact value returned by this routine.
  124.                 Only the sign (i.e. positive, zero, or negative) of the result is
  125.                 guaranteed.
  126.  
  127.     S64And, S64Or, S64Eor and S64Not
  128.     
  129.                 Return Boolean (1 or 0) depending on the outcome of the logical
  130.                 operation.
  131.  
  132.     S64BitwiseAnd, S64BitwiseOr, S64BitwiseEor and S64BitwiseNot
  133.     
  134.                 Return the Bitwise result.
  135.                 
  136.     S64ShiftRight and S64ShiftLeft
  137.     
  138.                 The lower 7 bits of the shift argument determines the amount of 
  139.                 shifting.  S64ShiftRight is an arithmetic shift while U64ShiftRight
  140.                 is a logical shift.
  141.  
  142.     SInt64ToLongDouble
  143.                 
  144.                 Converts SInt64 to long double.  Note all SInt64s fit exactly into 
  145.                 long doubles, thus, the binary -> decimal conversion routines
  146.                 in fp.h can be used to achieve SInt64 -> long double -> decimal
  147.                 conversions.
  148.                 
  149.     LongDoubleToSInt64
  150.     
  151.                 Converts a long double to a SInt64.  Any decimal string that fits
  152.                 into a SInt64 can be converted exactly into a long double, using the
  153.                 conversion routines found in fp.h.  Then this routine can be used
  154.                 to complete the conversion to SInt64.
  155.                 
  156.     SInt64ToWide
  157.     
  158.                 Converts a SInt64 to a wide struct.  If SInt64 is implemented
  159.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  160.                 implememnted as a long long, it casts the long long into a 
  161.                 wide struct.
  162.     
  163.     WideToSInt64
  164.     
  165.                 Converts a wide struct into a SInt64.  If SInt64 is implemented
  166.                 as a typedef of wide, the marco does nothing. If SInt64 is 
  167.                 implememnted as a long long, it reads the struct into a long long.
  168.     
  169.     
  170.     The corresponding UInt64 routines are also included.
  171.     
  172. --------------------------------------------------------------------------------*/
  173.  
  174.  
  175. #if TYPE_LONGLONG 
  176.  
  177. #define S64Max() 9223372036854775807
  178. #define S64Min() (-S64Max() - 1)
  179. #define S64Add(x, y) ((x) + (y))
  180. #define S64Subtract(x, y) ((x) - (y))
  181. #define S64Negate(x) (-(x))
  182. #define S64Absolute(x) absll((x))
  183. #define S64Multiply(x, y) ((x) * (y))
  184. #define S64Div(x, y) ((x) / (y))
  185. #define S64Mod(x, y) ((x) % (y))
  186. #define S64Set(x) ((SInt64) (x))
  187. #define S64SetU(x) ((SInt64) (x))
  188. #define S32Set(x) ((SInt32) (x))
  189. #define S64Compare(x, y) ((int)((x) - (y)))
  190. #define S64And(x, y) ((Boolean)((x) && (y)))
  191. #define S64Or(x, y) ((Boolean)((x) || (y)))
  192. #define S64Eor(x, y) ((Boolean)((x) ^ (y)))
  193. #define S64Not(x) ((Boolean)(!(x)))
  194. #define S64BitwiseAnd(x, y) ((x) & (y))
  195. #define S64BitwiseOr(x, y) ((x) | (y))
  196. #define S64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  197. #define S64BitwiseNot(x) (~(x))
  198. #define S64ShiftRight(x, y) ((x) >> (y))
  199. #define S64ShiftLeft(x, y) ((x) << (y))
  200. #define SInt64ToLongDouble(x) ((long double)(x))
  201. #define LongDoubleToSInt64(x) ((SInt64)(x))
  202.  
  203. #define U64Max() 0xffffffffffffffff
  204. #define U64Add(x, y) ((x) + (y))
  205. #define U64Subtract(x, y) ((x) - (y))
  206. #define U64Multiply(x, y) ((x) * (y))
  207. #define U64Div(x, y) ((x) / (y))
  208. #define U64Mod(x, y) ((x) % (y))
  209. #define U64Set(x) ((UInt64) (x))
  210. #define U64SetU(x) ((UInt64) (x))
  211. #define U32SetU(x) ((UInt32) (x))
  212. #define U64Compare(x, y) ((int)((x) - (y)))
  213. #define U64And(x, y) ((Boolean)((x) && (y)))
  214. #define U64Or(x, y) ((Boolean)((x) || (y)))
  215. #define U64Eor(x, y) ((Boolean)((x) ^ (y)))
  216. #define U64Not(x) ((Boolean)(!(x)))
  217. #define U64BitwiseAnd(x, y) ((x) & (y))
  218. #define U64BitwiseOr(x, y) ((x) | (y))
  219. #define U64BitwiseEor(x, y) (((x) & (~(y))) | ((~(x)) & (y)))
  220. #define U64BitwiseNot(x) (~(x))
  221. #define U64ShiftRight(x, y) ((x) >> (y))
  222. #define U64ShiftLeft(x, y) ((x) << (y))
  223. #define UInt64ToLongDouble(x) ((long double)(x))
  224. #define LongDoubleToUInt64(x) ((UInt64)(x))
  225. #define UInt64ToSInt64(x) ((SInt64)(x))
  226. #define SInt64ToUInt64(x) ((UInt64)(x))
  227.  
  228. #else  
  229.  
  230. EXTERN_API_C( SInt64 ) S64Max(void );
  231.  
  232. EXTERN_API_C( SInt64 ) S64Min(void );
  233.  
  234. EXTERN_API_C( SInt64 ) S64Add(SInt64 x, SInt64 y);
  235.  
  236. EXTERN_API_C( SInt64 ) S64Subtract(SInt64 left, SInt64 right);
  237.  
  238. EXTERN_API_C( SInt64 ) S64Negate(SInt64 value);
  239.  
  240. EXTERN_API_C( SInt64 ) S64Absolute(SInt64 value);
  241.  
  242. EXTERN_API_C( SInt64 ) S64Multiply(SInt64 xparam, SInt64 yparam);
  243.  
  244. #define S64Div(dividend, divisor) S64Divide(dividend, divisor, NULL)
  245.  
  246. EXTERN_API_C( SInt64 ) S64Divide(SInt64 dividend, SInt64 divisor, SInt64 *remainder);
  247.  
  248. EXTERN_API_C( SInt64 ) S64Set(SInt32 value);
  249.  
  250. EXTERN_API_C( SInt64 ) S64SetU(UInt32 value);
  251.  
  252. EXTERN_API_C( SInt32 ) S32Set(SInt64 value);
  253.  
  254. EXTERN_API_C( int ) S64Compare(SInt64 left, SInt64 right);
  255.  
  256. EXTERN_API_C( Boolean ) S64And(SInt64 left, SInt64 right);
  257.  
  258. EXTERN_API_C( Boolean ) S64Or(SInt64 left, SInt64 right);
  259.  
  260. EXTERN_API_C( Boolean ) S64Eor(SInt64 left, SInt64 right);
  261.  
  262. EXTERN_API_C( Boolean ) S64Not(SInt64 value);
  263.  
  264. EXTERN_API_C( SInt64 ) S64BitwiseAnd(SInt64 left, SInt64 right);
  265.  
  266. EXTERN_API_C( SInt64 ) S64BitwiseOr(SInt64 left, SInt64 right);
  267.  
  268. EXTERN_API_C( SInt64 ) S64BitwiseEor(SInt64 left, SInt64 right);
  269.  
  270. EXTERN_API_C( SInt64 ) S64BitwiseNot(SInt64 value);
  271.  
  272. EXTERN_API_C( SInt64 ) S64ShiftRight(SInt64 value, UInt32 shift);
  273.  
  274. EXTERN_API_C( SInt64 ) S64ShiftLeft(SInt64 value, UInt32 shift);
  275.  
  276. /*
  277.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  278. */
  279. EXTERN_API_C( long double ) SInt64ToLongDouble(SInt64 value);
  280.  
  281. EXTERN_API_C( SInt64 ) LongDoubleToSInt64(long double value);
  282.  
  283. EXTERN_API_C( UInt64 ) U64Max(void );
  284.  
  285. EXTERN_API_C( UInt64 ) U64Add(UInt64 x, UInt64 y);
  286.  
  287. EXTERN_API_C( UInt64 ) U64Subtract(UInt64 left, UInt64 right);
  288.  
  289. EXTERN_API_C( UInt64 ) U64Multiply(UInt64 xparam, UInt64 yparam);
  290.  
  291. #define U64Div(dividend, divisor) U64Divide(dividend, divisor, NULL)
  292.  
  293. EXTERN_API_C( UInt64 ) U64Divide(UInt64 dividend, UInt64 divisor, UInt64 *remainder);
  294.  
  295. EXTERN_API_C( UInt64 ) U64Set(SInt32 value);
  296.  
  297. EXTERN_API_C( UInt64 ) U64SetU(UInt32 value);
  298.  
  299. EXTERN_API_C( UInt32 ) U32SetU(UInt64 value);
  300.  
  301. EXTERN_API_C( int ) U64Compare(UInt64 left, UInt64 right);
  302.  
  303. EXTERN_API_C( Boolean ) U64And(UInt64 left, UInt64 right);
  304.  
  305. EXTERN_API_C( Boolean ) U64Or(UInt64 left, UInt64 right);
  306.  
  307. EXTERN_API_C( Boolean ) U64Eor(UInt64 left, UInt64 right);
  308.  
  309. EXTERN_API_C( Boolean ) U64Not(UInt64 value);
  310.  
  311. EXTERN_API_C( UInt64 ) U64BitwiseAnd(UInt64 left, UInt64 right);
  312.  
  313. EXTERN_API_C( UInt64 ) U64BitwiseOr(UInt64 left, UInt64 right);
  314.  
  315. EXTERN_API_C( UInt64 ) U64BitwiseEor(UInt64 left, UInt64 right);
  316.  
  317. EXTERN_API_C( UInt64 ) U64BitwiseNot(UInt64 value);
  318.  
  319. EXTERN_API_C( UInt64 ) U64ShiftRight(UInt64 value, UInt32 shift);
  320.  
  321. EXTERN_API_C( UInt64 ) U64ShiftLeft(UInt64 value, UInt32 shift);
  322.  
  323. /*
  324.     "long double" means 128 bit type on PowerPC and 80-bit type on 68K
  325. */
  326. EXTERN_API_C( long double ) UInt64ToLongDouble(UInt64 value);
  327.  
  328. EXTERN_API_C( UInt64 ) LongDoubleToUInt64(long double value);
  329.  
  330. EXTERN_API_C( SInt64 ) UInt64ToSInt64(UInt64 value);
  331.  
  332. EXTERN_API_C( UInt64 ) SInt64ToUInt64(SInt64 value);
  333.  
  334. #endif /* TYPE_LONGLONG */
  335.  
  336. /* 
  337.     Functions to convert between [Unsigned]Wide and [S|U]Int64 types.
  338.     
  339.     These functions are necessary if source code which uses both
  340.     wide and SInt64 is to compile under a compiler that supports
  341.     long long.
  342. */
  343. #if TYPE_LONGLONG 
  344.     #define SInt64ToWide(x)         (*((wide*)(&x)))
  345.     #define WideToSInt64(x)         (*((SInt64*)(&x)))
  346.     #define UInt64ToUnsignedWide(x) (*((UnsignedWide*)(&x)))
  347.     #define UnsignedWideToUInt64(x) (*((UInt64*)(&x)))
  348. #else
  349.     #define SInt64ToWide(x)         (x)
  350.     #define WideToSInt64(x)         (x)
  351.     #define UInt64ToUnsignedWide(x) (x)
  352.     #define UnsignedWideToUInt64(x) (x)
  353. #endif
  354.  
  355.  
  356.  
  357.  
  358. #if PRAGMA_STRUCT_ALIGN
  359.     #pragma options align=reset
  360. #elif PRAGMA_STRUCT_PACKPUSH
  361.     #pragma pack(pop)
  362. #elif PRAGMA_STRUCT_PACK
  363.     #pragma pack()
  364. #endif
  365.  
  366. #ifdef PRAGMA_IMPORT_OFF
  367. #pragma import off
  368. #elif PRAGMA_IMPORT
  369. #pragma import reset
  370. #endif
  371.  
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375.  
  376. #endif /* __MATH64__ */
  377.  
  378.